import {config as base_url} from '../config.js'
class Http{
  // 解构 对象的解构
  request({url, data={}, method="GET"}){
    return new Promise((resolve, reject)=>{
      this._request(url, resolve, reject, data, method)
    })
  }
  _request(url, resolve, reject, data = {}, method = "GET"){
    wx.request({
      url: base_url.api_base_url + url,
      method: method,
      data: data,
      header: {
        "content-type": "application/json",
        "appkey": base_url.appkey
      },
      success:res=>{
        const code = res.statusCode.toString()
        if (code.startsWith('2')){
          resolve(res.data)
        }else{
          reject()
          this._show_error()
        }
      },
      fail:err=>{
        reject()
        this._show_error()
      }
    })
  }

  _show_error(error_code){
    wx.showToast({
      title:'请求失败',
      icon: 'none',
      duration: 2000
    })
  }
}

export default Http